import os

from EmpVoting.EmpUtils import EmpNetworkConverter

class NetworkSerializer(object):

    def __init__(self, networkFormat, path="", fileName=""):
        '''
        Constructor
        '''
        
        self.Format = networkFormat
        
        if path != "":
            self.Path = path
            
        if fileName != "":
            self.FileName = fileName
       
    def Serialize(self, network):
        path = ""
        if hasattr(self, "Path"):
            path = self.Path
        else:
            path = os.getcwd()
        
        fileName = ""
        if hasattr(self, "FileName"):
            fileName = self.FileName
        else:
            fileName = "network." + self.Format
            
        fullPath = path + "\\" + fileName
            
        if self.Format == "gexf":
            EmpNetworkConverter.SaveEmpNetworkToGexf(network, fullPath)
        elif self.Format == "graphml":
            EmpNetworkConverter.SaveEmpNetworkToGraphMl(network, fullPath)
        elif self.Format == "gpickle":
            EmpNetworkConverter.SaveEmpNetworkToGpickle(network, fullPath)
        else:
            print("Error: Unknown network serialization format.")